package com.winning.sx.microframework.service.base.impl;

import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.service.base.SearchService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private EasyDao dao;

    @Override
    public Map<String, String> searchParamBuild(String keyWord, String orgAllow) throws Exception {
        Map<String, String> result = new HashMap();
        if (keyWord == null || "".equals(keyWord.trim())) {
            throw new Exception("关键词不能为空");
        }
        String keyWords[] = keyWord.split(" ");
        for (int i = 0; i < keyWords.length; i++) {
            String ids = null;
            ids = getOneCode(keyWords[i]);
            if (ids != null) {
                result.put("equipmentOneCode", ids);
                System.out.println("第" + i + "个词判断为资产类型：" + ids);
                continue;
            }
            ids = getConst(keyWords[i], "203");//资产状态
            if (ids != null) {
                result.put("equipmentState", ids);
                System.out.println("第" + i + "个词判断为资产状态：" + ids);
                continue;
            }
            ids = getConst(keyWords[i], "300");//品牌
            if (ids != null) {
                result.put("equipmentBrand", ids);
                System.out.println("第" + i + "个词判断为品牌：" + ids);
                continue;
            }
            ids = getConst(keyWords[i], "108");//使用对象类型
            if (ids != null) {
                result.put("useType", ids);
                System.out.println("第" + i + "个词判断为使用对象类型：" + ids);
                continue;
            }
            ids = getModelCode(keyWords[i]);//型号
            if (ids != null) {
                result.put("model", "'"+keyWords[i]+"'");//当时存储的不是id
                result.put("modelCode", ids);
                System.out.println("第" + i + "个词判断为型号：" + ids);
                continue;
            }
            ids = getDept(keyWords[i], orgAllow);
            if (ids != null) {
                result.put("dept", ids);
                System.out.println("第" + i + "个词判断为部门：" + ids);
                continue;
            }
            ids = getDate(keyWords[i]);
            if (ids != null) {
                result.put("useDate", ids);
                System.out.println("第" + i + "个词判断为领取日期：" + ids);
            }
            ids = getStaff(keyWords[i], orgAllow);
            if (ids != null) {
                result.put("staff", ids);
                System.out.println("第" + i + "个词判断为人员：" + ids);
            }
            ids = getQRCode(keyWords[i]);
            if (ids != null) {
                result.put("QRCode", ids);
                System.out.println("第" + i + "个词判断为二维码：" + ids);
            }
            ids = getWarehouseChange(keyWords[i]);
            if (ids != null) {
                result.put("changeFlowCode", ids);
                System.out.println("第" + i + "个词判断为库存变动单：" + ids);
            }
            ids = getEquipmentGet(keyWords[i]);
            if (ids != null) {
                result.put("getFlowCode", ids);
                System.out.println("第" + i + "个词判断为领取申请单：" + ids);
            }

        }
        return result;
    }

    //科室
    private String getDept(String keyWord, String orgAllow) throws SQLException {
        String sql = "select * from BASE_Organization o where o.name like '%" + keyWord + "%' and validFlag='100001' " +
                " and o.id in (" + orgAllow + ")";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum(); i++) {
                Map row = all.getDataRows().get(i);
                result += ("," + row.get("id").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //类型
    private String getOneCode(String keyWord) throws SQLException {
        String sql = "select * from BASE_EqClass where name like '%" + keyWord + "%'" +
                " and  validFlag = '100001' ";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum(); i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += (",'" + row.get("code").toString() + "'");
            }
            return result.substring(1);
        } else return null;
    }
    //型号
    private String getModelCode(String keyWord) throws SQLException {
        String sql = "select * from BASE_EquipmentDetail where (name like '%" + keyWord + "%' or  code like '%" + keyWord + "%')" +
                " and  validFlag = '100001' ";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum(); i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += (",'" + row.get("id").toString() + "'");
            }
            return result.substring(1);
        } else return null;
    }
    //常量
    private String getConst(String keyWord, String type) throws SQLException {
        String sql = "select * from base_const where type='" + type + "' and name like '%" + keyWord + "%'" +
                " and  validFlag = '100001' ";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum(); i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += ("," + row.get("code").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //人员
    private String getStaff(String keyWord, String orgAllow) throws SQLException {
        String where = " ( s.name LIKE '%" + keyWord + "%' OR s.job LIKE '%" + keyWord + "%') ";
        String isDate = getDate(keyWord);
        if (isNum(keyWord)) {
            where = " ( s.telephone LIKE '%" + keyWord + "%' OR s.telephone2 LIKE '%" + keyWord + "%' OR s.telephone3 LIKE '%" + keyWord + "%') ";
        } else if (isDate != null) {
            where = " s.birthday = '" + isDate + "' ";
        }
        String sql = "SELECT s.* FROM  BASE_Staff s LEFT JOIN  BASE_Organization o ON s.organizationId = o.id " +
                "WHERE s.validFlag = '100001' " + " and o.id in (" + orgAllow + ")" +
                "      AND  " + where;
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum()&&i<10; i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += ("," + row.get("id").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //二维码
    private String getQRCode(String keyWord) throws SQLException {
        if (!isNum(keyWord)) return null;
        String sql = "select * from BASE_QRcode where code like '%" + keyWord + "%'";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum()&&i<10; i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += ("," + row.get("id").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //库存变动单
    private String getWarehouseChange(String keyWord) throws SQLException {
        if (!isNum(keyWord)) return null;
        String sql = "select * from BUSI_WarehouseChange  where flowCode like '%" + keyWord + "%'" +
                "and validFlag='100001'";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum()&&i<10; i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += ("," + row.get("id").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //资产申请单
    private String getEquipmentGet(String keyWord) throws SQLException {
        if (!isNum(keyWord)) return null;
        String sql = "select * from BUSI_EquipmentGet  where flowCode like '%" + keyWord + "%'" +
                "and validFlag='100001'";
        DataTable all = dao.querySql(sql, new HashMap());
        String result = "";
        if (all.getTotalRowNum() > 0) {
            for (int i = 0; i < all.getTotalRowNum()&&i<10; i++) {
                Map row = (Map) all.getDataRows().get(i);
                result += ("," + row.get("id").toString());
            }
            return result.substring(1);
        } else return null;
    }

    //数字
    private boolean isNum(String keyWord) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        boolean isNum = pattern.matcher(keyWord).matches();
        return isNum;
    }

    //日期
    private String getDate(String keyWord) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-M-d");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月d日");
        String result = "";
        try {
            Date date = sdf1.parse(keyWord.trim());
            result = sdf2.format(date);
        } catch (Exception e) {
            result = "";
        }
        if ("".equals(result)) {
            try {
                Date date = sdf2.parse(keyWord.trim());
                result = sdf2.format(date);
            } catch (Exception e) {
                result = "";
            }
        }
        if ("".equals(result)) {
            try {
                Date date = sdf3.parse(keyWord.trim());
                result = sdf2.format(date);
            } catch (Exception e) {
                result = "";
            }
        }
        if ("".equals(result)) {
            try {
                Date date = sdf4.parse(keyWord.trim());
                result = sdf2.format(date);
            } catch (Exception e) {
                result = "";
            }
        }
        if ("".equals(result)) return null;
        return result;
    }


    @Test
    public void t() {
        String template1 = "2020年1月1日";
        String template2 = "2020年2月01日";
        String template3 = "2020-1-1";
        String template4 = "2020-2-01";
        try {
            System.out.println(getDate(template1));
            System.out.println(getDate(template2));
            System.out.println(getDate(template3));
            System.out.println(getDate(template4));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
